/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.PostAd.Controller;

import com.Auto.Common.PostAd.Hibernate.Postad;
import com.PostAd.Model.PostAd_Model;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Yogesh
 */
public class PostAd_Controller extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "PostAd";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        com.PostAd.Model.PostAd_Model ad_Model=(PostAd_Model) form;

        org.hibernate.SessionFactory sessionFactory=com.Auto.Common.PostAd.Hibernate.PostAd_HibernateUtil.getSessionFactory();
        org.hibernate.Session session=sessionFactory.openSession();
        org.hibernate.Transaction transaction=session.beginTransaction();

         String idFromUserInformation=ad_Model.getIdFromUserInformation();
        String postAd_Company_Name=ad_Model.getPostAd_Company_Name();
        String postAd_Address=ad_Model.getPostAd_Address();
        String postAd_Location=ad_Model.getPostAd_Location();
        String postAd_Mobile_Number=ad_Model.getPostAd_Mobile_Number();
        String postAd_Landline_Number=ad_Model.getPostAd_Landline_Number();
        String postAd_Image_Size=ad_Model.getPostAd_Image_Size();
        String postAd_Templet_Image=ad_Model.getPostAd_Templet_Image();
        String postAd_Ad_Id;
        String postAd_AmountToBePaid="0";
        String postAd_Ad_Apprval="0";
        String activationCode=ad_Model.getActivationCode();

          String str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                StringBuffer sb = new StringBuffer(5);

             for (int i=0;  i<5;  i++) {


                 int ndx = (int)(Math.random()*str.length());

                 sb.append(str.charAt(ndx));

             }

                postAd_Ad_Id=sb+"";

          com.Auto.Common.PostAd.Hibernate.Postad postad=new Postad(idFromUserInformation, postAd_Company_Name, postAd_Address, postAd_Location, postAd_Mobile_Number, postAd_Landline_Number, postAd_Image_Size,"VehicleHai/"+activationCode+"/PostAdvertisement", postAd_Ad_Id, postAd_AmountToBePaid, postAd_Ad_Apprval);
          session.save(postad);
          transaction.commit();
         
          new com.insurenceStep2.Controller.InsuranceImageStore(postAd_Templet_Image, activationCode,postAd_Ad_Id+".vhi","PostAdvertisement");

          request.setAttribute("data","PostAd");
        
        return mapping.findForward(SUCCESS);
    }
}
